//
//  HomeOptionCollectionViewCell.m
//  BigTyre
//
//  Created by 田涛 on 2017/3/28.
//  Copyright © 2017年 Lemon. All rights reserved.
//

#import "HomeOptionCollectionViewCell.h"

#import <SDAutoLayout.h>
#import <SDWebImage/UIImageView+WebCache.h>

@interface HomeOptionCollectionViewCell ()
@property (nonatomic, strong) UIImageView *img;
@property (nonatomic, strong) UILabel *name;
@property (nonatomic, strong) UILabel *price;
@property (nonatomic, strong) UILabel *price_yj;
@property (nonatomic, strong) UILabel *renqi;
@property (nonatomic, assign) CGFloat priceW;
@property (nonatomic, assign) CGFloat price_yiW;
@property (nonatomic, assign) CGFloat renqiW;
@end

@implementation HomeOptionCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addViews];
    }
    return self;
}

- (void)addViews {
    
    [self.contentView addSubview:self.img];
    [self.contentView addSubview:self.name];
    [self.contentView addSubview:self.price];
    [self.contentView addSubview:self.price_yj];
    [self.contentView addSubview:self.renqi];
}

- (void)layoutViews {
    _img.sd_layout
    .topEqualToView(self.contentView)
    .leftEqualToView(self.contentView)
    .rightEqualToView(self.contentView)
    .heightEqualToWidth(self.contentView);
    
    _name.sd_layout
    .topSpaceToView(_img,0)
    .leftSpaceToView(self.contentView,5)
    .rightSpaceToView(self.contentView,5)
    .autoHeightRatio(0.3);
    
    _price.sd_layout
    .topSpaceToView(_name,0)
    .leftSpaceToView(self.contentView,5)
    .widthIs(_priceW)
    .autoHeightRatio(0);
    
    _price_yj.sd_layout
    .topSpaceToView(_name,1)
    .leftSpaceToView(_price,0)
    .widthIs(_price_yiW)
    .autoHeightRatio(0);
    
    _renqi.sd_layout
    .topSpaceToView(_price_yj,5)
    .leftSpaceToView(self.contentView,5)
    .widthIs(_price_yiW)
    .autoHeightRatio(0);
}


#pragma mark - 懒加载
- (UIImageView *)img {
    if (!_img) {
        _img = [[UIImageView alloc] init];
    }
    return _img;
}

- (UILabel *)name {
    if (!_name) {
        _name = [[UILabel alloc] init];
        _name.font = [UIFont systemFontOfSize:14];
        _name.numberOfLines = 2;
    }
    return _name;
}

- (UILabel *)price {
    if (!_price) {
        _price = [[UILabel alloc] init];
        _price.textColor = [UIColor redColor];
        _price.font = [UIFont systemFontOfSize:20];
    }
    return _price;
}

- (UILabel *)price_yj {
    if (!_price_yj) {
        _price_yj = [[UILabel alloc] init];
        _price_yj.textColor = [UIColor lightGrayColor];
        _price_yj.font = [UIFont systemFontOfSize:18];
    }
    return _price_yj;
}

- (UILabel *)renqi {
    if (!_renqi) {
        _renqi = [[UILabel alloc] init];
        _renqi.textColor = [UIColor lightGrayColor];
        _renqi.font = [UIFont systemFontOfSize:16];
    }
    return _renqi;
}

- (void)setModel:(HomeOptionModel *)model {
    [_img sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:nil];
    _name.text = model.name;
    _price.text = [NSString stringWithFormat:@"¥%@",model.price];
    _price_yj.text = [NSString stringWithFormat:@"¥%@",model.price_yj];
    _renqi.text = [NSString stringWithFormat:@"人气%@",model.renqi];
    // 给_price_yi加删除线
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:_price_yj.text];
    [attri addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle)  range:NSMakeRange(0, _price_yj.text.length)];
    [attri addAttribute:NSStrikethroughColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0, _price_yj.text.length)];
    [_price_yj setAttributedText:attri];
    
    // 根据字的个数得到label的宽度
    NSDictionary *attrs = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:20]};
    CGSize size =[_price.text sizeWithAttributes:attrs];
    _priceW = size.width;
    
    // 根据字的个数得到label的宽度
    NSDictionary *attrs2 = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:18]};
    CGSize size2 =[_price_yj.text sizeWithAttributes:attrs2];
    _price_yiW = size2.width;
    
    NSDictionary *attrs3 = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:15]};
    CGSize size3 =[_renqi.text sizeWithAttributes:attrs3];
    _renqiW = size3.width;
    
    [self layoutViews];
}
@end
